Systems and Methods for Processing XML Document as a Stream of Events Using XPATH Context

ABSTRACT

An improved XML query system represents an XML document as a stream of discrete ‘events,’ with each event representing a portion of the document as the document is being parsed. Expression-based event matching such as XPath can be performed against the event stream using a stack or tree to keep only the relevant contexts such as XPath context in memory. Observers can be used to listen for matching events. Matching events can then be routed for processing by appropriate objects or components and returned to the event stream if necessary. This description is not intended to be a complete description of, or limit the scope of, the invention. Other features, aspects, and objects of the invention can be obtained from a review of the specification, the figures, and the claims.

CLAIM OF PRIORITY

This application is a continuation application of U.S. patentapplication Ser. No. 10/780,299 entitled SYSTEMS AND METHODS FORSTREAMING XPATH QUERY, filed on Feb. 17, 2004 (Attorney Docket No.BEAS-01330US1 SRM/DTX) which claims priority from U.S. ProvisionalApplication No. 60/451,313, entitled SYSTEMS AND METHODS FOR STREAMINGXPATH QUERY, by Patrick Calahan, filed on Feb. 28, 2003 (Attorney DocketNo. BEAS-01330US0 SRM/DTX) both of which are incorporated herein byreference in entirety.

COPYRIGHT NOTICE

A portion of the disclosure of this patent document contains materialwhich is subject to copyright protection. The copyright owner has noobjection to the facsimile reproduction by anyone of the patent documentof the patent disclosure, as it appears in the Patent and TrademarkOffice patent file or records, but otherwise reserves all copyrightrights whatsoever.

CROSS-REFERENCED CASES

The following application is cross-referenced and incorporated herein byreference in its entirety:

U.S. patent application Ser. No. 10/304,207 entitled “Streaming ParserAPI,” by Chris Fry et al., filed Nov. 26, 2002 (Attorney Docket No.BEAS-01100US1).

FIELD OF THE INVENTION

The present invention relates to the querying of data, such as from adocument or file.

BACKGROUND

XPath is a W3C language standard that can be used to address or queryparts of an XML document. It models an XML document as a tree of nodes,which can include element nodes, attribute nodes and/or text nodes.XPath can be used to identify a subset of an XML document by matching,or determining whether a node matches a pattern, similar to how SQL canbe used against a database. In the typical case, an expression writtenin the XPath language is evaluated against an XML document to determinewhich parts of the document ‘match’ the XPath. In order to do this, theXML document must be parsed and represented in memory. One of thestandard representations of XML is the Document Object Model (DOM). DOMmodel presents an XML document as a hierarchy of nodes through which onecan navigate arbitrarily. This approach provides a lot of flexibility,but comes at a cost in terms of efficiency and memory use, as the entiredocument must be brought into memory at one time.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a diagram showing an exemplary system that can be used inaccordance with one embodiment of the present invention.

FIG. 2 shows an exemplary data tree that can be used with the system ofFIG. 1 in an embodiment.

FIG. 3 is a flowchart for an exemplary process that can be used with thesystem of FIG. 1 in an embodiment.

DETAILED DESCRIPTION

The invention is illustrated by way of example and not by way oflimitation in the figures of the accompanying drawings in which likereferences indicate similar elements. It should be noted that referencesto “an” or “one” embodiment in this disclosure are not necessarily tothe same embodiment, and such references mean at least one.

Systems and methods in accordance with one embodiment of the presentinvention overcome deficiencies in existing XML query systems byrepresenting the XML document as a stream of discrete ‘events’, witheach event representing a portion of the document as the document isbeing parsed. Event matching can be performed against the event stream.Matching events can then be routed for processing by appropriate objectsor components and returned to the event stream if necessary.

XPath can be used to identify a subset of an XML document, similar tohow SQL can be used against a database. XPath is a W3C language standardthat can be used to address or query parts of an XML document. It canaddress parts of an XML document by providing basic facilities formanipulating strings, numbers, and Boolean variables. XPath operates onthe hierarchical structure, which can be but is not limited to a tree,instead of the syntax of an XML document and can be used for matching,or determining whether a node matches a pattern. It models an XMLdocument as a tree of nodes, which can include element nodes, attributenodes and/or text nodes and defines a way to compute a string-value foreach node type. The primary syntactic construct in XPath is theexpression. An expression is evaluated to yield an object of typenode-set, Boolean, number, or string. In the typical case, an expressionwritten in the XPath language is evaluated against an XML document todetermine which parts of the document ‘match’ the XPath. In order to dothis, the XML document must be parsed and represented in memory.

Systems and methods in accordance with one embodiment of the presentinvention adopt a true streaming approach, passing bits of an XMLdocument one after another, and it is up to the system to decide what todo with each bit as it passes on the stream. An advantage of a truestreaming approach is that such a system is faster and far more memoryefficient than a DOM-style approach, since only one portion of thedocument is in memory at any given time. When using a streaming parser,a system can take a stream on an XML document, generating a stream ofevents, one event for each node in the XML tree, and perform XPathmatching on that stream. A streaming XPath system can also be schemaaware, such that the system knows the XML schema for a document, thatschema can be used to provide insight on how to most effectively processthe document. For instance, the need to go “backwards” in a stream canbe avoided if the system knows in advance which events it needs to graband in what order those events will be received.

A streaming approach can place a greater burden on a system to maintainrelevant state than a DOM approach, as a streaming approach may provideno navigation mechanisms. While such an approach provides a veryefficient way to process an XML document, the efficiency comes at acost, as there can be considerably less context available when workingwith a stream than when working with a DOM tree. Further, XPath has tobe able to traverse the hierarchy, in some sense, in order to locate theappropriate portion of the document. In many instances, it is simple tolocate an appropriate portion of XML against a DOM tree, since thesystem is able to walk against the tree. When using a stream, a systemhas to maintain context in a way that is efficient enough to make usingthe stream worthwhile. Some tradeoffs can be made, such as notsupporting the entire XPath specification. At some point, it may be moreefficient to realize an entire DOM tree, if doing a convoluted matchingagainst the entire document.

The XPath specification defines the notion of a context, where a contextis the information about an event, consisting of a node it represents, aposition of the node relative to a parent node, and a function library,as well as any of several other components such as variable bindings. Alocation path is a type of expression that can select a set of nodesrelative to the context node. The evaluation of a location pathexpression can result in the node-set containing the nodes beingselected by the location path. Location paths can recursively containexpressions used to filter node sets. Expressions can be parsed by firstdividing the character string to be parsed into tokens, then parsing theresulting token sequence.

In one embodiment, it is relatively easy to map context to the stream,as the system can maintain a stack of stream events that provide thedirect ancestral line back to the root. For instance, matching an XPaththat consists solely of child axes can be straightforward. In anotherembodiment, mapping can become more complicated in the case ofdescendant axes, similar to matching an entire sub-tree. In those cases,it can be necessary to spawn a tree of contexts and perform matchingagainst each of those contexts. It can become complicated, as the systemgets to maintain, and know when you can discard those cloned contexts.It can be even more complicated when matching axes called “following,”which match everything below a certain point in the document. In somecases, it is necessary to maintain that context tree and track what toadd on to the tree as the system navigates its way back out of thedocument.

Systems and methods in accordance with one embodiment of the presentinvention know how to manage the multi-context mode discussed in theproceeding paragraph. They utilize the information of contexts in thestack matching against the expression to recognize when to go into thismulti-context mode, when to destroy those contexts, and how to updatethe context stack appropriately. Certain optimizations can also be usedthat can know when not to match certain contexts in the context tree.XPath defines different ways to slice up a document, such as parents andchildren, that each has to be dealt with in a different way.

Systems and methods in accordance with one embodiment do not account forreverse axes. A reverse axis is any axis that would require going “back”through the stream. A diagram showing an exemplary “forward” and“backward” or “reverse” path through a data tree is given by FIG. 2. Adiagram of an exemplary system is shown in FIG. 1. A streaming parser102 generates events by parsing an XML document 100, and then placesthose events on an XML event stream. Such a streaming process isdemonstrated by the diagram of FIG. 3. The streaming parser first takesa tree of an XML document as the input 300, traverses the XML treeeither through a broad-first search or a depth-first search and addseach node visited into a data structure, e.g., a queue 302. Thestreaming parser then processes the queue in the first-in-first-out(FIFO) manner 304 to generate an event for the context of each node inthe queue 306 and appends each event to the output stream 308. Using theevent stream, the end user of the streaming API pulls events from thestream as they come through it. When a user calls for the next event onthe stream, that user has a guarantee that they will get the next event.The user will find out if the next event is going to match, and willfind out before the call to next returns.

In one embodiment, an XPath matching component 104 performs matching oneach event received on the stream. Matching can be communicated to acaller or end user in a number of ways. These systems are doingevent-based processing, as opposed to static tree-based processing. In atree-based implementation, for example, a user can request all the nodesthat match an XPath for a document. The user will receive a collectionof nodes that match that XPath. Such an approach is not necessarilyeffective in the case of streaming, as it is then necessary to readthrough the document, save all the nodes, and present the collection tothe user. This is fundamentally not a stream-centric way of looking atthe problem. Instead, using an XPath matching approach, an observer 106can be registered. The registered observer is an object to be notifiedwhenever an event comes through the stream that matches this XPath. Ifan event matches an XPath, that event can be temporarily diverted andsent over to a user-defined object 108 that reacts to the match. Then,the event can be returned to the stream if necessary so that anysubsequent object pulling events from the stream can process that event.

One embodiment may be implemented using a conventional general purposeor a specialized digital computer or microprocessor(s) programmedaccording to the teachings of the present disclosure, as will beapparent to those skilled in the computer art. Appropriate softwarecoding can readily be prepared by skilled programmers based on theteachings of the present disclosure, as will be apparent to thoseskilled in the software art. The invention may also be implemented bythe preparation of integrated circuits or by interconnecting anappropriate network of conventional component circuits, as will bereadily apparent to those skilled in the art.

One embodiment includes a computer program product which is a storagemedium (media) having instructions stored thereon/in which can be usedto program a computer to perform any of the features presented herein.The storage medium can include, but is not limited to, any type of diskincluding floppy disks, optical discs, DVD, CD-ROMs, micro drive, andmagneto-optical disks, ROMs, RAMs, EPROMs, EEPROMs, DRAMs, VRAMs, flashmemory devices, magnetic or optical cards, nanosystems (includingmolecular memory ICs), or any type of media or device suitable forstoring instructions and/or data.

Stored on any one of the computer readable medium (media), the presentinvention includes software for controlling both the hardware of thegeneral purpose/specialized computer or microprocessor, and for enablingthe computer or microprocessor to interact with a human user or othermechanism utilizing the results of the present invention. Such softwaremay include, but is not limited to, device drivers, operating systems,execution environments/containers, and applications.

The foregoing description of the preferred embodiments of the presentinvention has been provided for the purposes of illustration anddescription. It is not intended to be exhaustive or to limit theinvention to the precise forms disclosed. Many modifications andvariations will be apparent to the practitioner skilled in the art.Embodiments were chosen and described in order to best describe theprinciples of the invention and its practical application, therebyenabling others skilled in the art to understand the invention, thevarious embodiments and with various modifications that are suited tothe particular use contemplated. It is intended that the scope of theinvention be defined by the following claims and their equivalents.

1. A system to process an XML document, comprising: a streaming parsercapable of parsing an XML document and generating a stream of at leastone event, wherein each event can represent a portion of the document; amatching component capable of performing matching on an event in thestream and notifying an observer if the event is a match; said observercapable of listening for a matching event and passing it to a userobject; said user object capable of handling the matching event; and thematching component is capable of maintaining context for each event in adata structure in order to locate the appropriate portion of thedocument.
 2. The system according to claim 1, wherein: the XML documentis represented in a hierarchical structure.
 3. The system according toclaim 2, wherein: the hierarchical structure can be a tree with eachnode containing a portion of the document.
 4. The system according toclaim 1, wherein: the context maintained by said matching component isXPath context.
 5. The system according to claim 1, wherein: said datastructure is a context stack, wherein new context can be added on saidcontext stack and each context in the said context stack can be matched.6. The system according to claim 1, wherein: said data structure is acontext tree, wherein new context can be added on said context stack andeach context in the said context tree can be matched.
 7. The systemaccording to claim 1, wherein: the matching component is capable ofkeeping only a portion of the XML document in memory at any given time.8. The system according to claim 1, wherein: the matching component iscapable of knowing the schema of the XML document and foreseeing thecoming events.
 9. The system according to claim 1, wherein: the matchingcomponent is capable of performing an expression-based match, which canbe an XPath query.
 10. The system according to claim 1, wherein: thematching component is capable of keeping, cloning and destroying theentirety or a portion of the sub-tree descending from a node in thetree.
 11. The system according to claim 1, wherein: the user object iscapable of returning the matching event to an XML stream for use by anyother component.
 12. A method for processing an XML document,comprising: parsing an XML document and generating a stream of at leastone event, wherein each event can represent a portion of the document;performing matching on an event in the stream and notifying an observerif the event is a match; listening for a matching event and passing itto a user object; handling the matching event; and maintaining contextfor each event in a data structure in order to locate the appropriateportion of the document.
 13. The method according to claim 12, furthercomprising: representing the XML document in a hierarchical structure,which can be a tree with each node containing a portion of the document.14. The method according to claim 12, wherein: the context maintained isXPath context.
 15. The method according to claim 12, wherein: the saiddata structure is a context stack, wherein new context can be added onsaid context stack and each context in the said context stack can bematched.
 16. The method according to claim 12, wherein: the said datastructure is a context tree, wherein new context can be added on saidcontext tree and each context in the said context tree can be matched.17. The method according to claim 12, further comprising: keeping only aportion of the XML document in memory at any given time.
 18. The methodaccording to claim 12, further comprising: knowing the schema of the XMLdocument and foreseeing the coming events.
 19. The method according toclaim 12, further comprising: performing an expression-based match,which can be an XPath query.
 20. The method according to claim 12,further comprising: keeping, cloning and destroying the entirety or aportion of the sub-tree descending from a node in the tree.
 21. Themethod according to claim 12, further comprising: returning the matchingevent to an XML stream for use by any other component.
 22. A method forlocating and matching a portion of the document while representing a XMLdocument in a stream of events, comprising: maintaining context for eachevent in a data structure; matching against each context
 23. The methodaccording to claim 22, further comprising: representing the XML documentin a hierarchical structure, which can be a tree with each nodecontaining a portion of the document.
 24. The method according to claim22, wherein: the context maintained is XPath context.
 25. The methodaccording to claim 22, wherein: said data structure is a context stack,wherein new context can be added on said context stack and each contextin the said context stack can be matched.
 26. The method according toclaim 22, wherein: said data structure is a context tree, wherein newcontext can be added on said context tree and each context in the saidcontext tree can be matched.
 27. The method according to claim 22,further comprising: keeping only a portion of the XML document in memoryat any given time.
 28. The method according to claim 22, furthercomprising: knowing the schema of the XML document and foreseeing thecoming events.
 29. The method according to claim 22, further comprising:performing an expression-based match, which can be an XPath query. 30.The method according to claim 22, further comprising: keeping, cloningand destroying the entirety or a portion of the sub-tree descending froma node in the tree.